package cn.meibang.aggragation;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.ResourcePropertySource;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ConsumerConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.ProviderConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.AnnotationBean;

@Configuration
public class DubboApestarConsumerConfig {

    ResourcePropertySource propertySource = null;

    public static final String ANNOTATION_PACKAGE = "cn.meibang";

    public void init() {
        if (propertySource == null)
            try {
                // 加载propertie
                propertySource = new ResourcePropertySource("resource", "classpath:dubbo.properties");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } // name, location
    }

    @Bean
    public ApplicationConfig applicationConfig() {
        init();
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName(propertySource.getProperty("dubbo.application_name").toString());
        return applicationConfig;
    }

    @Bean
    public RegistryConfig registryConfig() {
        init();
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setProtocol("zookeeper");
        registryConfig.setTimeout(3000);
        registryConfig.setCheck(Boolean.valueOf(propertySource.getProperty("dubbo.check").toString()));
        registryConfig.setRegister(Boolean.valueOf(propertySource.getProperty("dubbo.register").toString()));
        registryConfig.setAddress(propertySource.getProperty("dubbo.registry_address").toString() + "?register="
            + propertySource.getProperty("dubbo.register").toString());
        return registryConfig;
    }

    @Bean
    public AnnotationBean annotationBean() {
        init();
        AnnotationBean annotationBean = new AnnotationBean();
        annotationBean.setPackage(ANNOTATION_PACKAGE);
        if ("false".equals(propertySource.getProperty("dubbo.start").toString())) {
            annotationBean.setPackage("aaa.ccc.ddd");
        }
        return annotationBean;
    }

    @Bean(autowire = Autowire.BY_NAME, name = "dubbo")
    public ProtocolConfig protocolConfig() {
        init();
        ProtocolConfig config = new ProtocolConfig();
        config.setName("dubbo");
        config.setHost(propertySource.getProperty("dubbo.address").toString());
        config.setPort(Integer.parseInt(propertySource.getProperty("dubbo.provider").toString()));
        return config;
    }

    @Bean
    public ProviderConfig providerConfig() {
        init();
        ProviderConfig p = new ProviderConfig();
        p.setPort(Integer.parseInt(propertySource.getProperty("dubbo.provider").toString()));
        p.setProtocol("dubbo");
        return p;
    }

    @Bean
    public ConsumerConfig consumerConfig() {
        init();
        ConsumerConfig c = new ConsumerConfig();
        c.setCheck(false);
        return c;
    }

}